package com.sany.taskmanager.http.viewmodel;

import android.app.Activity;
import android.content.Intent;

import androidx.lifecycle.ViewModel;

import com.sany.taskmanager.TaskManagerApplication;
import com.sany.taskmanager.bean.BaseResponse;
import com.sany.taskmanager.bean.ColorBean;
import com.sany.taskmanager.bean.MaterialBean;
import com.sany.taskmanager.bean.SendTaskBean;
import com.sany.taskmanager.bean.TaskInfoBean;
import com.sany.taskmanager.global.Constants;
import com.sany.taskmanager.http.DataRepository;
import com.sany.taskmanager.http.IHttpCallBack;
import com.sany.taskmanager.ui.activity.LoginActivity;
import com.sany.taskmanager.util.GsonUtil;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class TaskViewModel extends ViewModel {
    private final DataRepository dataRepository;

    public TaskViewModel(DataRepository dataRepository) {
        this.dataRepository = dataRepository;
    }

    /**
     * 登录
     */
    public Disposable login(String userName, String password, String factoryCode, IHttpCallBack<String> callBack) {
        return subscribe(callBack, dataRepository.login(userName, password, "3G011", factoryCode));
    }


    /**
     * 退出登录
     */
    public Disposable logout(IHttpCallBack<Boolean> callBack) {
        return subscribe(callBack, dataRepository.logout());
    }

    /**
     * 获取颜色列表
     */
    public Disposable getColorList(IHttpCallBack<List<ColorBean>> callBack) {
        return subscribe(callBack, dataRepository.getColorList());
    }

    /**
     * 获取颜色列表：通过颜色编码过滤
     */
    public Disposable getColorListByCode(String colorCode, IHttpCallBack<List<ColorBean>> callBack) {
        return subscribe(callBack, dataRepository.getColorListByCode(colorCode));
    }

    /**
     * 获取三一标配颜色
     */
    public Disposable getSanyColorList(IHttpCallBack<List<ColorBean>> callBack) {
        return subscribe(callBack, dataRepository.getSanyColorList());
    }

    /**
     * 获取已下发任务
     */
    public Disposable getSendTaskList(IHttpCallBack<List<TaskInfoBean>> callBack) {
        return subscribe(callBack, dataRepository.getSendTaskList());
    }

    /**
     * 获取历史任务：包括已完成和已取消的任务
     */
    public Disposable getHistoryTaskList(IHttpCallBack<List<TaskInfoBean>> callBack) {
        return subscribe(callBack, dataRepository.getHistoryTaskList());
    }

    /**
     * 下发任务
     */
    public Disposable sendTask(List<SendTaskBean> taskList, IHttpCallBack<Boolean> callBack) {
        return subscribe(callBack, dataRepository.sendTask(GsonUtil.BeanToJson(taskList)));
    }

    /**
     * 修改任务
     */
    public Disposable modifyTask(String taskId, String materialNum, int colorType, int boomType, IHttpCallBack<Boolean> callBack) {
        return subscribe(callBack, dataRepository.modifyTask(taskId, materialNum, colorType, boomType));
    }

    /**
     * 取消任务
     */
    public Disposable cancelTask(String taskId, IHttpCallBack<Boolean> callBack) {
        return subscribe(callBack, dataRepository.cancelTask(taskId));
    }


    /**
     * 根据车型号获取物料清单
     */
    public Disposable getMaterialListByCarModelNum(String carModelNum, IHttpCallBack<List<MaterialBean>> callBack) {
        return subscribe(callBack, dataRepository.getMaterialListByCarModelNum(carModelNum));
    }

    /**
     * 抽离公共方法
     */
    private <T> Disposable subscribe(IHttpCallBack<T> callBack, Single<BaseResponse<T>> single) {
        return single.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(baseResponse -> {
            onSuccess(callBack, baseResponse);
        }, throwable -> {
            onFailed(callBack, throwable.getMessage());
        });
    }

    /**
     * 抽离公共方法：onSuccess
     */
    private <T> void onSuccess(IHttpCallBack<T> callBack, BaseResponse<T> response) {
        if (response.getCode() == 200) {
            if (callBack != null) {
                callBack.onSuccess(response.getResult());
            }
        } else if (response.getCode() == Constants.ErrorCode.CODE_TOKEN_EXPIRED || response.getCode() == Constants.ErrorCode.CODE_TOKEN_ERROR) {
            // token过期 || token解析错误，直接跳转到登录页面
            Activity currentActivity = TaskManagerApplication.getInstance().getCurrentActivity();
            if (currentActivity != null) {
                currentActivity.startActivity(new Intent(currentActivity, LoginActivity.class));
                currentActivity.finish();
            } else {
                Intent intent = new Intent(TaskManagerApplication.getInstance().getContext(), LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                TaskManagerApplication.getInstance().getContext().startActivity(intent);
            }
            onFailed(callBack, response.getMessage());
        } else {
            onFailed(callBack, response.getMessage());
        }
    }

    /**
     * 抽离公共方法：onFailed
     */
    private <T> void onFailed(IHttpCallBack<T> callBack, String message) {
        if (callBack != null) {
            callBack.onFailed(message);
        }
    }

}
